package everydayGrade.t2;

/**
 * @Author: Siantar
 * @Date: 2023-10-02-16:31
 * @Description: 1.0
 */
public class T0188 {
    public int maxProfit(int k, int[] prices) {
        int n = prices.length;
        int[][] dp = new int[n][k];
        int min = prices[0];
        for (int i = 1; i < n; i++) {
            min = Math.min(min, prices[i]);
            dp[i][0] = Math.max(dp[i - 1][0], prices[i] - min);
            for (int j = 1; j < k; j++) {
                dp[i][j] = dp[i][j-1];
                int max = prices[i];
                int maxDif=0;
                for (int l = i; l >= 1; l--) {
                    max = Math.max(max, prices[l]);
                    maxDif=Math.max(maxDif,max-prices[l]);
                    dp[i][j] = Math.max(dp[i][j], dp[l - 1][j - 1] + maxDif);
                }
            }
        }

        return dp[n-1][k-1];
    }
}
